package edu.jmu.transmissionassistant

import android.app.Application
import android.os.Environment
import com.yanzhenjie.andserver.util.IOUtils
import edu.jmu.transmissionassistant.component.AppConfig
import edu.jmu.transmissionassistant.component.MessageBus
import java.io.File


class App : Application() {

    var mRootDir: File? = null
        private set

    companion object {
        var mInstance: App? = null
            private set
    }

    override fun onCreate() {
        super.onCreate()
        if (mInstance == null) {
            mInstance = this
            initRootPath()
        }
        // register upload message bus
        MessageBus.instance().register(AppConfig.UPLOAD_PATH)
    }

    private fun initRootPath() {
        if (mRootDir != null) {
            return
        }

        val availableDir = File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOCUMENTS)
        mRootDir = availableDir.resolve("AndServer")
        IOUtils.createFolder(mRootDir)
        IOUtils.createFolder(mRootDir!!.resolve(AppConfig.UPLOAD_PATH))
    }
}